﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;
using DataInterfaceDLL;
using System.Threading;

namespace JKZP
{
    public partial class UploadForm : Form
    {
        DirectoryInfo di = null;
        FileInfo[] fis = null;
        int total = 0;
        int N = 0;
        public UploadForm()
        {
            InitializeComponent();
        }

        private void UploadForm_Load(object sender, EventArgs e)
        {
            try
            {
                di = new DirectoryInfo(PP.gJKRecordPath);
                if (di != null)
                {
                    di.Refresh();
                    fis = di.GetFiles("*.dat", SearchOption.AllDirectories);
                    if ((fis != null) && (fis.Length > 0))
                    {
                        N = 0;
                        total = fis.Length;
                        progressBar1.Maximum = total;
                        label_state.Text = "待上传" + total.ToString();
                    }
                }
            }
            catch { }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (PP.kdws == null)
                {
                    PP.kdws = new JKZP.aitmskdws.aitmskdws();
                    PP.kdws.Url = "http://" + PP.gServerIP + "/aitmskdws/aitmskdws.asmx";
                    PP.kdws.Discover();
                }
                if (fis != null)
                {
                    for (int i = 0; i < total; i++)
                    {
                        string name = fis[i].Name;
                        string ee = "";
                        bool b = DealOneRecord(fis[i], true, out ee);
                        if (b == true)
                        {
                            N++;
                            backgroundWorker1.ReportProgress(1, b.ToString() + ee + name);
                        }
                    }
                }
            }
            catch(Exception er)
            {
                backgroundWorker1.ReportProgress(0, er.Message);
            }
            
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                label_state.Text = N.ToString() + "/" + total.ToString();
                //toolStripStatusLabel1.Text = e.UserState.ToString();//太快了看不见效果
                progressBar1.PerformStep();
            }
            catch { }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                button_exit.Enabled = true;
                toolStripStatusLabel1.Text = "操作结束,共上传记录" + N.ToString() + "条";

                //工作量统计
                if (N > 0)
                {
                    if (PP.gISWebVersion == false)
                    {
                        PP.SaveGZL(PP.gUser.username, N);
                    }
                    else
                    { 
                        PP.kdws.JKZP_UploadGZL(PP.gUser.username, N);
                    }
                }
                N = 0;
            }
            catch { }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button_upload_Click(object sender, EventArgs e)
        {
            try
            {
                if (total > 0)
                {
                    button_exit.Enabled = false;
                    backgroundWorker1.RunWorkerAsync(fis);
                }
            }
            catch { button_exit.Enabled = true; }
        }

        private void UploadForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                backgroundWorker1.Dispose();
            }
            catch { }
        }

        private bool DealOneRecord(FileInfo fi, bool withpic,out string ee)
        {
            ee = "";
            bool ans = false;
            try
            {
                if (fi.Exists)
                {
                    string[] ss = fi.Name.Split('_');
                    if ((ss != null) && (ss.Length == 24))
                    {
                        RecordParam record = new RecordParam();
                        record.SnapCount = ss[0];
                        record.picJionMode = ss[1];
                        record.recordType = ss[2];
                        record.Company = ss[3];
                        record.captureDevice = ss[4];
                        record.deviceID = ss[5];
                        record.captureUser = ss[6];
                        record.WFXWDM = ss[7];
                        record.GBDM = ss[8];
                        record.BMBH = ss[9];
                        record.roadid = ss[10];
                        record.roadname = ss[11];
                        record.directionID = ss[12];
                        record.CD = ss[13];
                        record.JGSJ = ss[14];
                        record.RedLightBeginTime = ss[15];
                        record.RedLightDuration = ss[16];
                        record.plateType = ss[18];
                        record.ManuelCarType = ss[18];
                        record.plateColor = ss[19];
                        record.HPHM = ss[20];
                        record.bodyColor = ss[21];
                        record.speedLimit = ss[22];
                        record.speed = ss[23].Replace(".dat", "");

                        string[] paths = File.ReadAllLines(fi.FullName, Encoding.Default);
                        if ((paths != null) && (paths.Length == 6))//共6行
                        {
                            if (withpic == false)
                            {
                                if ((paths[0].StartsWith("http")) || (paths[0].StartsWith("ftp")))
                                {
                                    record.PIC1 = paths[0];
                                    record.PIC2 = paths[1];
                                    record.PIC3 = paths[2];
                                    record.PIC4 = paths[3];
                                    record.PICZoom = paths[4];
                                    record.PICPlate = paths[5];
                                }
                                else
                                {
                                    ee="withpic=false，但不包含http、ftp路径";
                                    return false;
                                }
                            }
                            else
                            {
                                if ((paths[0].StartsWith("http")) || (paths[0].StartsWith("ftp")))
                                {
                                    ee = "withpic=true，但不包含file路径";
                                    return false;
                                }
                                else
                                {
                                    record.PIC1 = paths[0];
                                    record.PIC2 = paths[1];
                                    record.PIC3 = paths[2];
                                    record.PIC4 = paths[3];
                                    record.PICZoom = paths[4];
                                    record.PICPlate = paths[5];
                                }
                            }
                        }
                        if (PP.kdws != null)
                        {
                            bool flag = false;
                            string error = "";
                            byte[] pic1 = null;
                            byte[] pic2 = null;
                            byte[] pic3 = null;
                            byte[] pic4 = null;
                            byte[] pic5 = null;
                            byte[] pic6 = null;
                            string name1 = "";
                            string name2 = "";
                            string name3 = "";
                            string name4 = "";
                            string name5 = "";
                            string name6 = "";
                            if (withpic == true)
                            {
                                try
                                {
                                    if ((paths[0] != "") && (File.Exists(paths[0])))
                                    {
                                        name1 = Path.GetFileName(paths[0]);
                                        pic1 = File.ReadAllBytes(paths[0]);
                                    }
                                    if ((paths[1] != "") && (File.Exists(paths[1])))
                                    {
                                        name2 = Path.GetFileName(paths[1]);
                                        pic2 = File.ReadAllBytes(paths[1]);
                                    }
                                    if ((paths[2] != "") && (File.Exists(paths[2])))
                                    {
                                        name3 = Path.GetFileName(paths[2]);
                                        pic3 = File.ReadAllBytes(paths[2]);
                                    }
                                    if ((paths[3] != "") && (File.Exists(paths[3])))
                                    {
                                        name4 = Path.GetFileName(paths[3]);
                                        pic4 = File.ReadAllBytes(paths[3]);
                                    }
                                    if ((paths[4] != "") && (File.Exists(paths[4])))
                                    {
                                        name5 = Path.GetFileName(paths[4]);
                                        pic5 = File.ReadAllBytes(paths[4]);
                                    }
                                    if ((paths[5] != "") && (File.Exists(paths[5])))
                                    {
                                        name6 = Path.GetFileName(paths[5]);
                                        pic6 = File.ReadAllBytes(paths[5]);
                                    }
                                }
                                catch(Exception er)
                                {
                                    ee = "读取图片异常" + er.Message;
                                    return false;
                                }
                            }

                            try
                            {
                                if (PP.gUploadPicOnly == false)
                                {
                                    if (withpic == true)
                                    {
                                        flag = PP.kdws.Upload(record.SnapCount, record.picJionMode, record.recordType,
                                            record.Company, record.captureDevice, record.deviceID, record.captureUser,
                                            record.WFXWDM, record.GBDM, record.BMBH, record.roadid, record.roadname,
                                            record.directionID, record.CD, record.yyyy, record.mm, record.dd, record.hh,
                                            record.mi, record.ss, record.zzz, record.RedLightBeginTime,
                                            "0", record.plateType, record.plateColor, record.HPHM,
                                            record.bodyColor, record.speedLimit, record.speed, name1, name2,
                                            name3, name4, name5, name6, true, pic1, pic2,
                                            pic3, pic4, pic5, pic6, out error);
                                    }
                                    else
                                    {
                                        flag = PP.kdws.Upload(record.SnapCount, record.picJionMode, record.recordType,
                                                record.Company, record.captureDevice, record.deviceID, record.captureUser,
                                                record.WFXWDM, record.GBDM, record.BMBH, record.roadid, record.roadname,
                                                record.directionID, record.CD, record.yyyy, record.mm, record.dd, record.hh,
                                                record.mi, record.ss, record.zzz, record.RedLightBeginTime,
                                                "0", record.plateType, record.plateColor, record.HPHM,
                                                record.bodyColor, record.speedLimit, record.speed, record.PIC1, record.PIC2,
                                                record.PIC3, record.PIC4, record.PICZoom, record.PICPlate, false, null, null,
                                                null, null, null, null, out error);
                                    }
                                }
                                else
                                {
                                    flag = PP.kdws.UploadPicOnly(record.recordType, record.roadname, 
                                        record.yyyy,record.mm, record.dd, name1, name2, name3, name4, 
                                        name5, name6, pic1, pic2, pic3, pic4, pic5, pic6, out error);
                                }
                            }
                            catch(Exception er)
                            {
                                ee = "kdws.Upload异常" + er.Message;
                                return false;
                            }

                            if (flag == false)
                            {
                                ee = "上传失败" + error;
                                ans = false;
                            }
                            else
                            {
                                if (withpic == true)
                                {
                                    try
                                    {
                                        //暂不删除，留着回看使用
                                        //if (paths[0] != "") File.Delete(paths[0]);
                                        //if (paths[1] != "") File.Delete(paths[1]);
                                        //if (paths[2] != "") File.Delete(paths[2]);
                                        //if (paths[3] != "") File.Delete(paths[3]);
                                        //if (paths[4] != "") File.Delete(paths[4]);
                                        //if (paths[5] != "") File.Delete(paths[5]);
                                    }
                                    catch { }
                                }

                                try
                                {
                                    fi.Delete();
                                }
                                catch { }

                                ans = true;
                            }
                        }
                    }
                }
            }
            catch(Exception er)
            {
                ee = "DealOneRecord异常" + er.Message;
                ans = false;
            }
            return ans;
        }
    }
}
